//
//  PopoverAnimator.swift
//  zyqWeibo
//
//  Created by 张亚琼 on 15/6/29.
//  Copyright © 2015年 张亚琼. All rights reserved.
//

import UIKit

class PopoverAnimator: NSObject,UIViewControllerAnimatedTransitioning,UIViewControllerTransitioningDelegate {
    
    // 要展示的视图的大小
    var presentFrame = CGRectZero
    // 是否展现
    var isPresent = false
    
    
        // 返回负责转场的控制器对象，即自定义的转场控制器
        func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
            
            let pc = PopoverPresentController(presentedViewController: presented, presentingViewController: presenting)
            // 设置frame
            pc.presentFrame = presentFrame
            return pc
        }
        
        // 返回提供modal动画的对象，即当前控制器
        func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
            // 展现
            isPresent = true
            // UIViewControllerAnimatedTransitioning
            return self
        }
        
        // 返回dismiss动画的对象
        func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
            isPresent = false
            return self
        }
        
        // 返回动画时长
        func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
            return isPresent ? 1.2 : 0.25
        }
        
        /// 转场动画实现函数，一旦实现该函数，所有的动画都失效。动画只能由程序控制
        func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
            
            // 从哪个控制器
            let frmoVC = transitionContext.viewControllerForKey(UITransitionContextFromViewKey)
            // 到哪个控制器
            let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewKey)
            
            print("从这个控制器出发：\(frmoVC)")
            print("到这个控制器：\(toVC)")
            
            //
            if isPresent {
                // 实现动画
                let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
                /// 将toView添加到容器视图
                transitionContext.containerView()?.addSubview(toView)
                /// 实现动画
                // 设置初始值，为了在动画中展开
                toView.transform = CGAffineTransformMakeScale(1.0, 0)
                // 设置锚点
                toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
                
                // 设置动画
                UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
                    toView.transform = CGAffineTransformMakeScale(1.0, 1.0)
                    }, completion: { (_) -> Void in
                        // 动画完成之后要通知系统已完成
                        transitionContext.completeTransition(true)
                })
            }else {
                // 取消
                let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
                
                UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                    fromView.transform = CGAffineTransformMakeScale(1.0, 0.0001)
                    }, completion: { (_) -> Void in
                        fromView.removeFromSuperview()
                        // 告诉系统已经结束
                        transitionContext.completeTransition(true)
                })
                
            }
        }
    

}
